import Vue from 'vue'
import VueRouter from 'vue-router'
import { resError } from '@/common/js/global'
import { getItem, getToken } from '@/common/utils/cookieUtil' // 验权
import store from '../store'
Vue.use(VueRouter)

const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

/**
 * 静态路由
 */
export const constantRouterMap = [
    {
        path: '/dashboard',
        name: "dashboard",
        meta: {
            icon: "el-icon-s-home",
            title: "桌面",
        },
        hidden: false,
        sort: null,
        component: () => import('@/views/dashboard'),
    },
    {
        path: '/login',
        meta: {
            icon: null,
            title: "登录",
        },
        hidden: true,
        sort: null,
        component: () => import('@/views/login'),
    },
    // {
    //     path: '*',
    //     meta: {
    //         icon: null,
    //         title: "404",
    //     },
    //     hidden: true,
    //     sort: null,
    //     component: () => import('@/404'),
    // },


]
/**
 * 动态路由
 */
export const asyncRouterMap = [

    {
        path: '/user',
        name: "user",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "userList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/user/user-list'),
            },

        ]
    },
    {
        path: '/file',
        name: "file",
        meta: {
            icon: "el-icon-s-order",
            title: "文件管理",
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "fileList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/file/file-list'),
            },
            {
                path: 'share',
                name: "shareFileList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/file/share-list'),
            },

        ]
    },
    {
        path: '/comment',
        name: "comment",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "commentList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/comment/comment-list'),
            },
            {
                path: 'sensitive',
                name: "sensitiveList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/comment/sensitive'),
            },

        ]
    },
    {
        path: '/im',
        name: "im",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'chat',
                name: "chatList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/im/chat-list'),
            },
            {
                path: 'contacts',
                name: "contactsList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/im/contacts-list'),
            },
            {
                path: 'group',
                name: "groupList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/im/group-list'),
            },
            {
                path: 'chat-detail',
                name: "chatDetailList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/im/chat-detail-list'),
            },
        ]
    },
    {
        path: '/notice',
        name: "notice",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "noticeList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/notice/notice-list'),
            },
            {
                path: 'send',
                name: "noticeAdd",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/notice/send'),
            }
        ]
    },
    {
        path: '/feedback',
        name: "feedback",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'list',
                name: "feedbackList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/feedback/feedback-list'),
            },
        ]
    },
    {
        path: '/permission-menu',
        name: "permsMenu",
        meta: {
            icon: null,
            title: null
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'user',
                name: "userPerms",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/user-permission'),
            },
            {
                path: 'role',
                name: "rolePerms",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/role-permission'),
            },
            {
                path: 'perms-list',
                name: "permsList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/perms-list'),
            },
            {
                path: 'add-perms',
                name: "addPerms",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/add-permission'),
            },
            {
                path: 'menu-list',
                name: "menuList",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/menu-list'),
            },
            {
                path: 'add-menu',
                name: "addMenu",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/permission-menu/add-menu'),
            },
        ]
    },
    {
        path: '/system',
        name: "system",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'setting',
                name: "systemSetting",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/system/setting'),
            },
            {
                path: 'login-log',
                name: "loginLog",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/system/login-log'),
            },

        ]
    },
    {
        path: '/api-doc',
        name: "apiDoc",
        meta: {
            icon: null,
            title: null,
        },
        hidden: null,
        sort: null,
        component: { render: (e) => e("router-view") },
        children: [
            {
                path: 'page',
                name: "apiDocPage",
                meta: {
                    icon: null,
                    title: null,
                },
                hidden: null,
                sort: null,
                component: () => import('@/views/api-doc/page'),
            },

        ]
    },
]


const router = new VueRouter({
    mode: 'history',
    routes: constantRouterMap,
    // 控制路由跳转滚动条的位置为最顶部
    scrollBehavior(to, from, savedPosition) {
        // 始终滚动到顶部，y代表距离顶部的距离
        return { y: 0 }
    },
})

const whiteList = ['/login'] // 不重定向白名单

router.beforeEach((to, from, next) => {
    if (getItem("token")) {
        if (to.path === '/login') {
            next({ path: '/dashboard' })
        } else {
            if (store.state.user.role.length === 0) {
                // 拉取用户信息
                getUserInfo(next)
            } else {
                next()
            }
        }
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
        }
    }
    next()

    // 保存历史记录
    if (from.path != '/') {
        store.commit("saveNavHistory", to)
    }
})

async function getUserInfo(next) {
    // 获取用户信息
    let res = await store.dispatch('getUserInfo', {
        id: null
    })
    if (res.success) {
        let menus = null, username = null
        menus = res.data.menuList;
        username = res.data.username;
        // 生成可访问的路由表
        store.dispatch('GenerateRoutes', { menus, username }).then(() => {
            // 动态添加可访问路由表
            router.addRoutes(store.state.menu.addRouters);
            next()
        })
    }
    else {
        // 退出登录
        // let res = await store.dispatch('logout')
        // if (res.success) {
        //     Message.error(err || 'Verification failed, please login again')
        //     next({ path: '/' })
        // }
        resError(res)
    }

}


export default router